package i2.application.checklist.struts.actions;

import i2.application.checklist.codes.AlerteNiveau;
import i2.application.checklist.entities.Categorie;
import i2.application.checklist.entities.Question;
import i2.application.checklist.services.CategorieService;
import i2.application.checklist.services.QuestionService;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;


public class QuestionAction extends ActionSupport {
	
	private static final long serialVersionUID = 1L;
	private static final Logger logger = LoggerFactory.getLogger(QuestionAction.class);

	private QuestionService questionService;
	private CategorieService categorieService;
	
	private List<Question> questionList;
	
	private Question question;
	
	private int id;
	
	private List<Categorie> categorieList;
	
	// TODO justement peut être déplacer ça dans le service ???
	private List<Categorie> enumCategorie;

	private boolean creation = false;
	
	public String consulter() {
		if (id == 0) {
			logger.error("aucun identifiant présent en paramètre");
			return ERROR;
		}

		logger.debug("consultation de l'application d'id : {}",id);
		
		question = questionService.getQuestion(new Question(id));
		
		
		if (question == null) {
			logger.error("{} n'est un identifiant connu",id);
			return ERROR;
		}
		
		return "view";
	}
	
	public String list() {
		categorieList = categorieService.getAllCategoriesWithQuestions();
		return "list";
	}
	
	public String creer() {
		creation = true;
		return INPUT;
	}
	
	public String enregistrer() {
		questionService.createQuestion(question);
		return SUCCESS;
	}
	
	public String editer() {
		question = questionService.getQuestion(question);
		
		if (question == null) {
			logger.error("{} n'est un identifiant connu",id);
			return ERROR;
		}
		
		return INPUT;
	}
	
	public String modifier() {
		questionService.updateQuestion(question);
		return SUCCESS;
	}
	
	public String supprimer() {
		
		question = questionService.getQuestion(question);
		
		if (question == null) {
			logger.error("{} n'est un identifiant connu",id);
			return ERROR;
		}
		
		questionService.deleteQuestion(question);
		return SUCCESS;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Question getQuestion() {
		return question;
	}

	public void setQuestion(Question Question) {
		this.question = Question;
	}
	
	public List<Question> getQuestionList() {
		return questionList;
	}

	public void setQuestionService(QuestionService questionService) {
		this.questionService = questionService;
	}

	public List<Categorie> getCategorieList() {
		return categorieList;
	}

	public void setCategorieService(CategorieService categorieService) {
		this.categorieService = categorieService;
	}

	public boolean isCreation() {
		return creation;
	}

	public List<Categorie> getEnumCategorie() {
		return enumCategorie;
	}

	public AlerteNiveau[] getEnumNiveauAlerte() {
		return AlerteNiveau.values();
	}

	public void setEnumCategorie(List<Categorie> enumCategorie) {
		this.enumCategorie = enumCategorie;
	}
	
}
